package main;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Calendar;

import kernel_db.parser.Parser;

import common.entities.persistent.Movie;


public class iniParams {
	private String serverPath; 
	private String userName;
	private String password;
	private int maxMovieYear;
	private int maxBatch;
	private int fetchSize;
	private String moviesFilePath;
	private String ratingsFilePath;
	private String countriesFilePath;
	private String genresFilePath;
	private String actorsFilePath;
	private String actressesFilePath;
	private String directorsFilePath;
	
	public iniParams() {
		// the default values
		this.serverPath = "jdbc:mysql://localhost:3306/DbMysql12";
		this.userName = "DbMysql12";
		this.password = "daniel87";
		Calendar cal=Calendar.getInstance();
		this.maxMovieYear = cal.get(Calendar.YEAR);
		this.maxBatch = 1000;
		this.fetchSize = 1000;
	}


	public String getServerPath() {
		return serverPath;
	}
	public void setServerPath(String serverPath) {
		this.serverPath = serverPath;
	}

	public String getUserName() {
		return userName;
	}
	public void setUserName(String userName) {
		this.userName = userName;
	}

	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}

	public int getMaxMovieYear() {
		return maxMovieYear;
	}
	public void setMaxMovieYear(int maxMovieYear) {
		this.maxMovieYear = maxMovieYear;
	}

	public String getMoviesFilePath() {
		return moviesFilePath;
	}
	public void setMoviesFilePath(String moviesFilePath) {
		this.moviesFilePath = moviesFilePath;
	}

	public String getRatingsFilePath() {
		return ratingsFilePath;
	}
	public void setRatingsFilePath(String ratingsFilePath) {
		this.ratingsFilePath = ratingsFilePath;
	}

	public String getCountriesFilePath() {
		return countriesFilePath;
	}
	public void setCountriesFilePath(String countriesFilePath) {
		this.countriesFilePath = countriesFilePath;
	}

	public String getGenresFilePath() {
		return genresFilePath;
	}
	public void setGenresFilePath(String genresFilePath) {
		this.genresFilePath = genresFilePath;
	}


	public String getActorsFilePath() {
		return actorsFilePath;
	}

	public void setActorsFilePath(String actorsFilePath) {
		this.actorsFilePath = actorsFilePath;
	}


	public String getActressesFilePath() {
		return actressesFilePath;
	}

	public void setActressesFilePath(String actressesFilePath) {
		this.actressesFilePath = actressesFilePath;
	}


	public String getDirectorsFilePath() {
		return directorsFilePath;
	}

	public void setDirectorsFilePath(String directorsFilePath) {
		this.directorsFilePath = directorsFilePath;
	}

		public int getMaxBatch() {
		return maxBatch;
	}

	public void setMaxBatch(int maxBatch) {
		this.maxBatch = maxBatch;
	}

	public int getFetchSize() {
		return fetchSize;
	}

	public void setFetchSize(int fetchSize) {
		this.fetchSize = fetchSize;
	}


	/**
	 * validates the min/max movie year values.
	 */
	public void validateMinMaxMovieYear() {
		if (maxMovieYear <= Movie.releaseYearMinValue) {
			Calendar cal=Calendar.getInstance();
			maxMovieYear = cal.get(Calendar.YEAR);
		}
		if (maxMovieYear > 2100)
			maxMovieYear = 2100;
	}

	/**
	 * read and parse the ini file.
	 * @return an iniParam with the ini file content.
	 */
	public static iniParams readINIFile(String[] args) {
		iniParams params = new iniParams();
		String iniFilePath = "";
		if (args.length == 0)
			iniFilePath = "millionaire.ini";
		else
			iniFilePath = args[0];
		
	
		// open file
		FileInputStream fileStream = null;
		try {
			fileStream = new FileInputStream(iniFilePath);
		} catch (FileNotFoundException e1) { 
			return params;
		}
		BufferedReader br = new BufferedReader(new InputStreamReader(fileStream));	
			
		try {	
			// parsing ini file
			String line = "";
			String paramName;
			String stringParam = null;
			int intParam = 0;
			boolean isParamOk;
			while (true && line != null) {
				line = br.readLine();
				
				if (line == null || line.length() <= 5 || line.charAt(0) != '-')
					continue;
				
				//getting the parameter name
				paramName = Parser.parseINIParamName(line);
				if (paramName == null)
					continue;
				
				//getting the parameter value
				isParamOk = false;
				if (paramName.equals("-server_path") || paramName.equals("-server_username") || paramName.equals("-server_password") ||
					paramName.equals("-movies_file_path") || paramName.equals("-actors_file_path") || paramName.equals("-actresses_file_path") ||
					paramName.equals("-directors_file_path") || paramName.equals("-countries_file_path") || paramName.equals("-genres_file_path") ||
					paramName.equals("-ratings_file_path") ) {
						
					stringParam = Parser.parseINIStringParam(line);
					if ((stringParam.length() != 0) || 
						(paramName.equals("-server_password") && stringParam.equals("")))
							isParamOk = true;
				}
				else if (paramName.equals("-movie_max_year") || paramName.equals("-max_batch") || paramName.equals("-fetch_size")) {
					intParam = Parser.parseINIIntParam(line);
					if (intParam != -1)
						isParamOk = true;
				}
				
				if (isParamOk == false)
					continue;
				
				//setting the parameter value
				if (paramName.equals("-server_path")) 
						params.setServerPath(stringParam);
				else if (paramName.equals("-server_username"))
					params.setUserName(stringParam);
				else if (paramName.equals("-server_password"))
					params.setPassword(stringParam);
				else if (paramName.equals("-movies_file_path"))
					params.setMoviesFilePath(stringParam);
				else if (paramName.equals("-actors_file_path"))
					params.setActorsFilePath(stringParam);
				else if (paramName.equals("-actresses_file_path"))
					params.setActressesFilePath(stringParam);
				else if (paramName.equals("-directors_file_path"))
					params.setDirectorsFilePath(stringParam);
				else if (paramName.equals("-countries_file_path"))
					params.setCountriesFilePath(stringParam);
				else if (paramName.equals("-genres_file_path"))
					params.setGenresFilePath(stringParam);
				else if (paramName.equals("-ratings_file_path"))
					params.setRatingsFilePath(stringParam);
				
				else if (paramName.equals("-movie_max_year"))
					params.setMaxMovieYear(intParam);
				else if (paramName.equals("-max_batch"))
					params.setMaxBatch(intParam);
				else if (paramName.equals("-fetch_size"))
					params.setFetchSize(intParam);
			
			}
						
		} catch (IOException e) { /* meaning: end of file */ }

		// closing files
		try {
			br.close();
			fileStream.close();
		} catch (IOException e) { }
		
		
		params.validateMinMaxMovieYear();
		
		return params;
	}
	
	

}
